using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core;
using Web.Core.Objects;
using System.Globalization;

public partial class AdminCP_Members : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Language"] == null)
            Session["Language"] = Web.Core.GlobalStorage.Language.English;

        int total = 1; int page = 1;
        if (Request.Params["page"] != null)
            page = int.Parse(Request.Params["page"]);
        ArrayList arrMembers;
        if (Request.Params["act"] != "search")
        {
            arrMembers = Members.GetMembersByPage(page, GlobalStorage.membersPerPageForAdmin);
            total = Members.CountAllMembers();
        }
        else
        {
            string mail = "", sex = "", fname = "", lname = "", dbirth = "", address = "", phone = "", squestion = "";
            Role role = new Role();
            #region gan' gia' tri mem
            if (Request.Params["mail"] != null)
            {
                mail = Request.Params["mail"];
            }
            if (Request.Params["sex"] != null)
            {
                sex = Request.Params["sex"];
            }
            if (Request.Params["fn"] != null)
            {
                fname = Request.Params["fn"];
            }
            if (Request.Params["ln"] != null)
            {
                lname = Request.Params["ln"];
            }
            if (Request.Params["dbirth"] != null)
            {
                dbirth = Request.Params["dbirth"];
                dbirth = dbirth.Replace("2F%", "/");
            }
            if (Request.Params["address"] != null)
            {
                address = Request.Params["address"];
            }
            if (Request.Params["ph"] != null)
            {
                phone = Request.Params["ph"];
            }
            if (Request.Params["sq"] != null)
            {
                squestion = GetSecretQuestion(int.Parse(Request.Params["sq"]));
            }
            if (Request.Params["role"] != null)
            {
                role = Roles.GetRole(int.Parse(Request.Params["role"]), "");
            }
            #endregion gan' gia' tri mem

            Member mem = new Member(0, mail, "", GetSex(sex), fname, lname, dbirth, address, phone, "", squestion, "", role, "");
            arrMembers = Members.GetMembers(mem, page, (int)GlobalStorage.membersPerPageForAdmin);
            total = Members.CountAllMembers(mem);
        }
        #region 1
        if (!IsPostBack)
        {
            this.rptMembers.DataSource = arrMembers;
            this.rptMembers.DataBind();
            InitControl();
        }
        // Pager
        this.Pager1.objectsPerPage = (int)GlobalStorage.membersPerPageForAdmin;
        this.Pager1.displayAll = false; // Chi hien thi ben phai
        this.Pager1.totalObjects = total;
        // ---------------

        if (Session["Membership"] != null && !(Session["Membership"] as Member).IsAdministrator())
        {
            foreach (RepeaterItem ri in this.rptMembers.Items)
            {
                Control ctrl = ri.FindControl("pnlDel");
                ctrl.Visible = false;
            }
        }

        if (Request.Params["do"] != null)
        {
            string mDo = Request.Params["do"];
            switch (mDo)
            {
                case "edit":
                    if (Request.Params["mid"] != null)
                        LoadControls();
                    break;
                case "add":
                    LoadControls();
                    if (Session["Membership"] != null && !(Session["Membership"] as Member).IsAdministrator())
                        Response.Redirect("Default.aspx");
                    break;
                case "del":
                    if (Session["Membership"] != null && !(Session["Membership"] as Member).IsAdministrator())
                        Response.Redirect("Default.aspx");
                    if (Request.Params["mid"] != null)
                    {
                        int memID = 0;
                        try
                        {
                            memID = int.Parse(Request.Params["mid"].ToString());
                        }
                        catch { }
                        Members.DeleteMember(Members.GetMemberByID(memID));
                        this.PreRenderComplete += new EventHandler(AdminCP_Members_PreRenderComplete);
                    }
                    break;
                default:
                    break;
            }
        #endregion
        }
    }

    void AdminCP_Members_PreRenderComplete(object sender, EventArgs e)
    {
        Response.Redirect("Members.aspx");
    }

    void LoadControls()
    {
        Control addEditMembers = (Control)Page.LoadControl("~/AdminCP/AdminControls/AddEditMembers.ascx");
        this.PlaceHolderBottom.Controls.Add(addEditMembers);
    }

    void InitControl()
    {
        this.btsearch.Text = Resources.Lang.Search;
        AddSecretQuestion();
        ArrayList roles = Roles.GetAllRoles();
        this.drpSRoles.Items.Clear();
        this.drpSRoles.Items.Add("");
        foreach (Role r in roles)
            this.drpSRoles.Items.Add(r.Name);        
    }
    void AddSecretQuestion()
    {
        this.drpSQuestions.Items.Clear();
        this.drpSQuestions.Items.Add("");
        CultureInfo temp = null;
        if (Resources.Lang.Culture != null)
            temp = Resources.Lang.Culture;
        Resources.Lang.Culture = new CultureInfo("en");
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourPet);
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourWord);
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourLife);
        Resources.Lang.Culture = new CultureInfo("vi");
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourPet);
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourWord);
        this.drpSQuestions.Items.Add(Resources.Lang.SecretQuestionYourLife);
        if (temp != null)
            Resources.Lang.Culture = temp;
    }
    protected void btsearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(HandleUrl());
    }    
    GlobalStorage.Sex GetSex(string sex)
    {
        if (sex == "Both")
            return GlobalStorage.Sex.Both;
        else
        {
            if (sex == "Male")
                return GlobalStorage.Sex.Male;
            else
                return GlobalStorage.Sex.Female;
        }
    }
    string HandleUrl()
    {
        string url = "Members.aspx?act=search";
        if (txtSEmail.Text != "")
            url += "&mail=" + txtSEmail.Text;
        url += "&sex=" + rblSex.SelectedValue;
        if (txtSFName.Text != "")
            url += "&fn=" + txtSFName.Text;
        if (txtSLName.Text != "")
            url += "&ln=" + txtSLName.Text;
        if (txtSBirthdate.Text != "")
        {
            txtSBirthdate.Text = txtSBirthdate.Text.Replace("/", "%2F");
            url += "&dbirth=" + txtSBirthdate.Text;
        }
        if (txtSAddress.Text != "")
            url += "&address=" + txtSAddress.Text;
        if (txtSPhone.Text != "")
            url += "&ph=" + txtSPhone.Text;
        if (drpSQuestions.SelectedIndex != 0)
            url += "&sq=" + drpSQuestions.SelectedIndex.ToString();
        if (drpSRoles.SelectedValue != "")
        {
            Role role = Roles.GetRole(0, drpSRoles.SelectedValue);
            url += "&role=" + role.ID;
        }
        return url;
    }
    string GetSecretQuestion(int id)
    {
        string sq = "";
        if (1 <= id && id <= 3)
        {
            Resources.Lang.Culture = new CultureInfo("en");
            if (id == 1)
                sq = Resources.Lang.SecretQuestionYourPet;
            else
            {
                if (id == 2)
                    sq = Resources.Lang.SecretQuestionYourWord;
                else
                    sq = Resources.Lang.SecretQuestionYourLife;
            }
        }
        else
        {
            Resources.Lang.Culture = new CultureInfo("vi");
            if (id == 4)
                sq = Resources.Lang.SecretQuestionYourPet;
            else
            {
                if (id == 5)
                    sq = Resources.Lang.SecretQuestionYourWord;
                else
                    sq = Resources.Lang.SecretQuestionYourLife;
            }
        }
        return sq;
    }   
}
